package view;

import assets.Icons;
import component.button.MaterialButton;
import component.edittext.PasswordEdit;
import component.edittext.TextEdit;
import control.Controllers;
import control.UserControl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
  * @Description 注册对话框
  * @author 孔明浩
  * @Date 2020/3/21
  */
public class RegisterDialog {
    /**
     * @Description
     * @param owner 对话框所属
     * @param parentContainer 对话框父组件
     * @author 孔明浩
     * @Date 2020/3/31
     */
    public static void showRegisterDialog(Frame owner, Container parentContainer){
        JDialog dialog = new JDialog(owner,true);
        dialog.setSize(350,280);
        dialog.setResizable(false);
        dialog.setLocationRelativeTo(parentContainer);
        dialog.setTitle("注册账户");
        dialog.setIconImage(Icons.register.getImage());

        JButton confirm = MaterialButton.BlueButton("确认注册",null);
        TextEdit usernameEdit = new TextEdit("请输入用户名");
        PasswordEdit passwordEdit = new PasswordEdit("请输入注册密码");
        PasswordEdit confirmPassword = new PasswordEdit("请确认密码");

        confirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (usernameEdit.getText().length() == 0){
                    usernameEdit.setErrorText("用户名不能为空");
                    passwordEdit.setErrorText("");
                    confirmPassword.setErrorText("");
                } else if (passwordEdit.getText().length() == 0){
                    passwordEdit.setErrorText("密码不能为空");
                    usernameEdit.setErrorText("");
                    confirmPassword.setErrorText("");
                } else if (!confirmPassword.getText().equals(passwordEdit.getText())){
                    confirmPassword.setErrorText("两次输入密码不一致");
                    usernameEdit.setErrorText("");
                    passwordEdit.setErrorText("");
                } else {
                    usernameEdit.setErrorText("");
                    passwordEdit.setErrorText("");
                    confirmPassword.setErrorText("");

                    //启动新线程查询数据
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            UserControl userControl = Controllers.USER_CONTROL;
                            String result = userControl.register(usernameEdit.getText(), passwordEdit.getText());
                            if ("yes".equals(result)){
                               showMessage(owner, "注册成功");
                               SwingUtilities.invokeLater(new Runnable() {
                                   @Override
                                   public void run() {
                                       dialog.dispose();
                                   }
                               });
                            } else {
                                showMessage(owner, "注册失败");
                            }
                        }
                    }).start();
                }
            }
        });

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        Box vBox = Box.createVerticalBox();
        vBox.add(usernameEdit);
        vBox.add(passwordEdit);
        vBox.add(confirmPassword);
        vBox.add(new JLabel(" "));
        panel.add(vBox);
        panel.add(confirm);
        dialog.setContentPane(panel);

        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setUndecorated(false);
        dialog.setVisible(true);
    }

    static void showMessage(Frame owner, String message){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JOptionPane.showMessageDialog(owner, message);
            }
        });
    }
}
